package net.ikenway.springboot.demo.Service.Implement;

import net.ikenway.springboot.demo.Dao.ActivityRepository;
import net.ikenway.springboot.demo.Dao.ProductRepository;
import net.ikenway.springboot.demo.Model.Dto.ActivityDto;
import net.ikenway.springboot.demo.Model.Entity.Activity;
import net.ikenway.springboot.demo.Model.Entity.Product;
import net.ikenway.springboot.demo.Service.Interface.ActivityServiceIntf;
import net.ikenway.springboot.demo.Utils.MapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/20/2020
 */
@Service
public class ActivityServiceImpl implements ActivityServiceIntf {
    private final ActivityRepository activityRepository;
    private final ProductRepository productRepository;
    private final MapperUtil mapperUtil;

    @Autowired
    ActivityServiceImpl(ActivityRepository activityRepository,
                        ProductRepository productRepository,
                        MapperUtil mapperUtil) {
        this.activityRepository = activityRepository;
        this.productRepository = productRepository;
        this.mapperUtil = mapperUtil;
    }

    @Override
    public List<ActivityDto> findAll() throws Exception {
        List<Activity> activities = activityRepository.findAll(Sort.by(Sort.Direction.ASC, "displayOrder"));
//        List<ActivityDto> activityDtos = mapperUtil.mapList(activities, ActivityDto.class);
//        for (Activity activity : activities) {
//            for (Product product : activity.getProducts()) {
//            }
//        }
//
//       List<ActivityDto> activityDtos = mapperUtil.mapList(activities, ActivityDto.class);
//        for (int i = 0; i < activities.size(); i++) {
//            ActivityDto activityDto = activityDtos.get(i);
//            Activity activity = activities.get(i);
//
//            activityDto.setProductIds(new ArrayList<>());
//
//            for (Product product : activity.getProducts()) {
//                activityDto.getProductIds().add(product.getProductId());
//            }
//        }

        return mapperUtil.mapList(activities, ActivityDto.class);
    }

    @Override
    public List<ActivityDto> findAllByEnable(Boolean enable) throws Exception {
        return mapperUtil.mapList(activityRepository.findAllByEnableOrderByDisplayOrder(enable), ActivityDto.class);
    }

    @Override
    public ActivityDto create(ActivityDto activityDto) throws Exception {
        if (activityDto.getEnable() && activityDto.getDisplayOrder() == null) throw new Exception("启用的活动显示顺序不能为空");
        Activity activity = mapperUtil.map(activityDto, Activity.class);
        activity.setProducts(new ArrayList<>());
        for (Long productId : activityDto.getProductIds()) {
            Product product = productRepository.findById(productId).orElseThrow(EntityNotFoundException::new);
            product.setDisCount(activity.getDisCount());
            activity.getProducts().add(product);
        }
        return mapperUtil.map(activityRepository.save(activity), ActivityDto.class);
    }

    @Override
    public void update(ActivityDto activityDto) throws Exception {
        if (activityDto.getEnable() && activityDto.getDisplayOrder() == null) throw new Exception("启用的活动显示顺序不能为空");
        Activity activity = mapperUtil.map(activityDto, Activity.class);

        List<Product> products = productRepository.findAllById(activityDto.getProductIds());
        if (products.isEmpty()) throw new EntityNotFoundException();
//        activityDto.setProducts(products);
        activity.setProducts(products);

        if (!activity.getEnable()) {
            for (Product product : products) {
                product.setDisCount(new BigDecimal(1));
            }
            activity.setDisplayOrder(null);
        } else {
            for (Product product : products) {
                product.setDisCount(activity.getDisCount());
            }
        }

//        for (Product product : activityDto.getProducts()) {
//            for (int i = 0; i < activity.getProducts().size(); i++) {
//                Product product1 = activity.getProducts().get(i);
//                if (product1.getProductId().equals(product.getProductId())) break;
//                // 如果在数据库的数据中没有找到该商品
//                // 则把商品假如到数据库中
//                // 即是活动新增的商品
//                if (i == activity.getProducts().size()) activity.getProducts().add(product);
//            }
//        }
//
//        for (Product product : activity.getProducts()) {
//            for (int i = 0; i < activityDto.getProducts().size(); i++) {
//                Product product1 = activityDto.getProducts().get(i);
//                if (product1.getProductId().equals(product.getProductId())) break;
//                // 如果在提交的表单数据中没有找到该商品
//                // 则把商品从数据库数据中移除
//                // 即是从该活动中移除
//                if (i == activity.getProducts().size()) activity.getProducts().remove(product);
//            }
//        }

        activityRepository.save(activity);
    }

    @Override
    public void disable(List<Long> actIds) throws Exception {
        List<Activity> activities = activityRepository.findAllById(actIds);
        if (activities.isEmpty()) throw new EntityNotFoundException();
        for (Activity activity : activities) {
            activity.setEnable(false);
            for (Product product : activity.getProducts()) {
                product.setDisCount(new BigDecimal(1));
            }
        }
        activityRepository.saveAll(activities);
    }

    @Override
    public void enable(List<Long> actIds) throws Exception {
        List<Activity> activities = activityRepository.findAllById(actIds);
        if (activities.isEmpty()) throw new EntityNotFoundException();
        for (Activity activity : activities) {
            activity.setEnable(true);
            for (Product product : activity.getProducts()) {
                product.setDisCount(activity.getDisCount());
            }
        }
        activityRepository.saveAll(activities);
    }


    @Override
    public void delete(List<Long> actIds) throws Exception {
        List<Activity> activities = activityRepository.findAllById(actIds);
        for (Activity activity : activities) {
            for (Product product : activity.getProducts()) {
                product.setDisCount(new BigDecimal(1));
            }
        }
        if (activities.isEmpty()) throw new EntityNotFoundException();
        activityRepository.deleteAll(activities);
    }
}
